package myJs.components

import japgolly.scalajs.react.ReactMonocle._
import japgolly.scalajs.react._
import japgolly.scalajs.react.component.Scala.BackendScope
import japgolly.scalajs.react.extra.StateSnapshot
import japgolly.scalajs.react.vdom.all._
import monocle.Lens
import monocle.macros.Lenses
import myJs.Implicits._
import myJs.Utils.layer
import myJs.components.inline.SelectInlineComponent
import myJs.myPkg.LayerOptions
import myJs.tool.Tool._
import myJs.user.FeatureSelectionTool
import org.scalajs.dom.html
import shared.Pojo._
import shared.SharedTool

import scala.collection.immutable.SeqMap

/** Created by yz on 3/11/2020
  */

object ScaleComponent {

  @Lenses
  case class State(standardMethod: String, labelWidth: Int = 3, inputWidth: Int = 3)

  object State {

    def init = {
      State(standardMethod = "scale")
    }

    implicit val resuabilityState: Reusability[State] = Reusability.derive

  }

  class Backend(bs: BackendScope[Unit, State]) {

    private val selectRef = Ref[html.Select]

    def render(s: State) = {

      implicit class MyLens[T](lens: Lens[State, T]) {

        def zoomL(implicit r: Reusability[T]) = {
          StateSnapshot.withReuse.zoomL(lens).prepareVia(bs).apply(s)
        }

      }

      div(
        SelectInlineComponent
          .Props(
            State.standardMethod.zoomL,
            inputWidth = s.inputWidth,
            labelWidth = s.labelWidth,
            label = messages("scaleMethod"),
            name = "scaleMethod",
            optionMap = scaleMethodMap
          )
          .render
      )

    }

  }

  def component(state: State) = {
    ScalaComponent.builder[Unit].initialState(state).renderBackend[Backend].build
  }

  val Component = component(State.init)

}
